import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
import Home from '../views/Home.vue'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
	return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/Search')
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import('@/views/Category')
  },
  {
    path: '/cart',
    name: 'Cart',
    meta: {
      isLogin: true
    },
    component: () => import('@/views/Cart')
  },
  {
    path: '/my',
    name: 'My',
    meta: {
      isLogin: true
    },
    component: () => import('@/views/My')
  },
  {
    path: "/login",
    name: "Login",
    component: () => import('@/views/Login')
  },
  {
    path: "/goodlist/:id",
    name: "GoodList",
    props: true,
    component: () => import('@/views/GoodList')
  },
  {
    path: "/detail/:id",
    name: "Detail",
    props: true,
    // meta:{hidenNav:true},
    component: () => import('@/views/Detail')
  },
  {
    path: "/comfire",
    name: "Comfire",
    component: () => import('@/views/Comfire')
  },
  {
    path: "/user/add_manage",
    name: "Add_Manage",
    component: () => import('@/views/user/Add_Manage')
  },
  {
    path: "/collection",
    name: "Collection",
    component: () => import('@/views/Collection')
  },
  {
    path: "/user/get_coupon",
    name: "Get_Coupon",
    component: () => import('@/views/user/Get_Coupon')
  },
  {
    path: "/successpay",
    name: "SuccessPay",
    component: () => import('@/views/SuccessPay')
  },
  {
    path: "/watchorder",
    name: "WatchOrder",
    component: () => import('@/views/WatchOrder')
  },
  {
    path: "/order/list/:index",
    name: "List",
    props: true,
    component: () => import('@/views/order/List')
  },
  {
    path: "*",
    name: "NotFound",
    component: () => import('@/views/NotFound')
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.isLogin) {
    if (!store.getters.token) {
      next('/login')
    }
  }
  next();
})

export default router
